<?php

class CMail{
	
	private $mail=null;
	
	private $transport=null;
	
	private $config=null;
	
	public function __construct($code='utf-8'){
		
		$this->mail=new Zend_Mail($code);
		
		$this->config=new CConfiguration();
			
		$EMAIL_HOST=$this->config->getDetail('EMAIL_HOST');
		
		$EMAIL_NAME=$this->config->getDetail('EMAIL_NAME');
		
		$EMAIL_PASSWORD=$this->config->getDetail('EMAIL_PASSWORD');
		
		$config = array('auth' => 'login',
            			'username' => $EMAIL_NAME,
            			'password' => $EMAIL_PASSWORD
    					);
    	
    	//$this->transport = new Zend_Mail_Transport_Smtp($EMAIL_HOST,$config);
		
	}
	
	public function sendRegisterMail($email){
		
		try{	
			
			$message=$this->config->getDetail('REGISTER_EMAIL_BODY');
		
			$subject=$this->config->getDetail('REGISTER_EMAIL_SUBJECT');
			
			$EMAIL_NAME=$this->config->getDetail('EMAIL_NAME');
			
			$this->mail->setBodyHtml($message);
				
			$this->mail->setFrom($EMAIL_NAME,"=?UTF-8?B?".base64_encode('fancycheapjersey.com')."?=");
			
			$this->mail->addTo($email);
			
			$this->mail->setSubject("=?UTF-8?B?".base64_encode($subject)."?=");
			
			$this->mail->send();
		
		}catch(Exception $e){
			
			return $e->getMessage();
			
		}
		
		return '';
		
	}
	
	public function sendRegisterMailSync($email){
		
		//proc_close(proc_open("/home/zicheng/script/mail www.jerseybbs.com " . $email . ' reg &', array(), $foo));
		$fp = fsockopen("www.fancycheapjersey.com", 80, $errno, $errstr, 30);
		if (!$fp) {   
			echo "$errstr ($errno)<br />\n";
		} else { 
			$out = "GET /auth-mail?mail=" . $email . "&type=reg  HTTP/1.1\r\n";
			$out .= "Host: www.fancycheapjersey.com\r\n";
			$out .= "Connection: Close\r\n\r\n";
			fwrite($fp, $out);   
			fclose($fp);
		}
	}
	
	public function sendMatchMail($email, $nickname, $team, $time, $dollar, $isWin)
	{
		try {
			if ($isWin == 0) {
				$message = $this->config->getDetail('MATCH_FAILURE_BODY');
				$subject = $this->config->getDetail('MATCH_FAILURE_SUBJECT');
			} else {
				$message = $this->config->getDetail('MATCH_WIN_BODY');
				$subject = $this->config->getDetail('MATCH_WIN_SUBJECT');
			}
			$message = preg_replace('/{nickname}/', $nickname, $message);
			$message = preg_replace('/{team}/', $team, $message);
			$message = preg_replace('/{matchtime}/', $time, $message);
			$message = preg_replace('/{dollar}/', $dollar, $message);
			
			$subject = preg_replace('/{nickname}/', $nickname, $subject);
			$subject = preg_replace('/{team}/', $team, $subject);
			$subject = preg_replace('/{matchtime}/', $time, $subject);
			$subject = preg_replace('/{dollar}/', $dollar, $subject);			
			
			$EMAIL_NAME = $this->config->getDetail('EMAIL_NAME');
			
			$this->mail->setBodyHtml($message);
			$this->mail->setFrom($EMAIL_NAME,"=?UTF-8?B?".base64_encode('fancycheapjersey.com')."?=");
			$this->mail->addTo($email);
			$this->mail->setSubject("=?UTF-8?B?".base64_encode($subject)."?=");
			$this->mail->send($this->transport);
			return true;
		} catch (Exception $e) {
			return $e->getMessage();
		}
	}
	
	public function sendEmail($from_email,$to_email,$subject,$content){
		
		try{
			$this->mail->setBodyHtml($content);
				
			$this->mail->setFrom($from_email,"=?UTF-8?B?".base64_encode('fancycheapjersey.com')."?=");
			
			$this->mail->addTo($to_email);
			
			$this->mail->setSubject("=?UTF-8?B?".base64_encode($subject)."?=");
			
			$this->mail->send($this->transport);
		
		}catch(Exception $e){
			
			return $e->getMessage();
			
		}
		
		return 'success';
		
	}
	
	
	
}
